<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>vue-router的创建过程 | 诶无痕和诶有痕</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Vue Router的使用（插件）功能12345678910嵌套路由映射动态路由选择模块化、基于组件的路由配置路由参数、查询、通配符展示由 Vue.js 的过渡系统提供的过渡效果细致的导航控制自动激活 CSS 类的链接HTML5 history 模式或 hash 模式可定制的滚动行为URL 的正确编码 安装12npm i vue-routervue add router 使用vue add rou">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-router的创建过程">
<meta property="og:url" content="http://example.com/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/index.html">
<meta property="og:site_name" content="诶无痕和诶有痕">
<meta property="og:description" content="Vue Router的使用（插件）功能12345678910嵌套路由映射动态路由选择模块化、基于组件的路由配置路由参数、查询、通配符展示由 Vue.js 的过渡系统提供的过渡效果细致的导航控制自动激活 CSS 类的链接HTML5 history 模式或 hash 模式可定制的滚动行为URL 的正确编码 安装12npm i vue-routervue add router 使用vue add rou">
<meta property="og:locale" content="zh_CH">
<meta property="article:published_time" content="2022-03-22T14:02:45.000Z">
<meta property="article:modified_time" content="2022-03-22T14:07:52.748Z">
<meta property="article:author" content="诶无痕和诶有痕">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="诶无痕和诶有痕" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">诶无痕和诶有痕</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-vue-router的创建过程" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/" class="article-date">
  <time datetime="2022-03-22T14:02:45.000Z" itemprop="datePublished">2022-03-22</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      vue-router的创建过程
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Vue-Router的使用（插件）"><a href="#Vue-Router的使用（插件）" class="headerlink" title="Vue Router的使用（插件）"></a>Vue Router的使用（插件）</h1><h2 id="功能"><a href="#功能" class="headerlink" title="功能"></a>功能</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">嵌套路由映射</span><br><span class="line">动态路由选择</span><br><span class="line">模块化、基于组件的路由配置</span><br><span class="line">路由参数、查询、通配符</span><br><span class="line">展示由 Vue.js 的过渡系统提供的过渡效果</span><br><span class="line">细致的导航控制</span><br><span class="line">自动激活 CSS 类的链接</span><br><span class="line">HTML5 history 模式或 hash 模式</span><br><span class="line">可定制的滚动行为</span><br><span class="line">URL 的正确编码</span><br></pre></td></tr></table></figure>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm i vue-router</span><br><span class="line">vue add router</span><br></pre></td></tr></table></figure>
<p>使用vue add router 安装时全选yes（回车）就可以了，安装完成后会自动一个router文件和view文件</p>
<p>使用npm i vue-router安装时，所有的相关配置都要自己手动配置</p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>以npm i vue-router安装为例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import App from &#x27;./App.vue&#x27;</span><br><span class="line">import router from &#x27;./router&#x27;</span><br><span class="line"></span><br><span class="line">// 配置调试信息</span><br><span class="line">Vue.config.productionTip = false</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  render: h =&gt; h(App)</span><br><span class="line">&#125;).$mount(&#x27;#app&#x27;)</span><br><span class="line"></span><br><span class="line">需要在main.js中注册router组件</span><br></pre></td></tr></table></figure>
<h3 id="路由表：路由表放在router文件中"><a href="#路由表：路由表放在router文件中" class="headerlink" title="路由表：路由表放在router文件中"></a>路由表：路由表放在router文件中</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import VueRouter from &#x27;vue-router&#x27;</span><br><span class="line">import HomeView from &#x27;../views/HomeView.vue&#x27;</span><br><span class="line"></span><br><span class="line">//通过vue.use的方式注册路由</span><br><span class="line">Vue.use(VueRouter)</span><br><span class="line"></span><br><span class="line">// 路由表，配置路由相关信息</span><br><span class="line">const routes = [</span><br><span class="line">  &#123;</span><br><span class="line">    path: &#x27;/&#x27;,</span><br><span class="line">    name: &#x27;home&#x27;,</span><br><span class="line">    // 这样的引用方式是静态引入</span><br><span class="line">    component: HomeView,</span><br><span class="line">    // 写数据，当前HomeView下可以拿到这些数据</span><br><span class="line">    // 在HomeView组件中，的mounted()&#123;&#125;生命周期中获取，也可以直接通过&#123;&#123;mustarche&#125;&#125;语法使用</span><br><span class="line">    // 通过this.$route获取(路由表中数据)数据</span><br><span class="line">    meta:&#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    path: &#x27;/right&#x27;,</span><br><span class="line">    name: &#x27;content&#x27;,</span><br><span class="line">    component: ()=&gt; </span><br><span class="line">    // 动态引入</span><br><span class="line">    // 这个注释是用来标注引入模块是什么名字</span><br><span class="line">    import(/* webpackChunkName: &quot;content&quot; */&quot;../components/RightContent.vue&quot;)</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  &#123;</span><br><span class="line">    path: &#x27;/right1&#x27;,</span><br><span class="line">    // 重定项</span><br><span class="line">    redirect:&quot;/right&quot;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  // 动态路由</span><br><span class="line">  &#123;</span><br><span class="line">    //  /:id动态传值,这里有值了后,前面使用router-link跳转时要传值</span><br><span class="line">    // 例如:&quot;/post/13&quot;,  这里的13就是动态数据</span><br><span class="line">    // 静态的内容不能变,动态的数据必须传</span><br><span class="line">    path:&quot;/post/:id&quot;,</span><br><span class="line">    component: ()=&gt; </span><br><span class="line">    // 这个注释是用来标注引入模块是什么名字</span><br><span class="line">    import(/* webpackChunkName: &quot;post&quot; */&quot;../views/post/post.vue&quot;)</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  // 嵌套路由,路由中套路由</span><br><span class="line">  &#123;</span><br><span class="line">    path:&quot;/post/:id&quot;,</span><br><span class="line">    component: ()=&gt; </span><br><span class="line">    import(/* webpackChunkName: &quot;post&quot; */&quot;../views/post/post.vue&quot;),</span><br><span class="line">    // 嵌套路由要在children中配置,</span><br><span class="line">    // 路径中有&quot;/&quot;会被当成根路径</span><br><span class="line">    // </span><br><span class="line">    children:[</span><br><span class="line">      &#123;</span><br><span class="line">        path:&quot;:id&quot;,</span><br><span class="line">        // 在post.vue中放一个router-view可以通过嵌套路由,将要嵌套的postDetail.vue映射过去</span><br><span class="line">        // component:import(/* webpackChunkName: &quot;postDetail&quot; */&quot;../views/post/postDetail.vue&quot;),</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">// 在这里实例化路由，并将路由表给实例化对象，</span><br><span class="line">// 还可以在这里配置相关的信息，</span><br><span class="line">const router = new VueRouter(&#123;</span><br><span class="line">  mode: &#x27;history&#x27;,</span><br><span class="line">  base: process.env.BASE_URL,</span><br><span class="line">  routes</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">export default router</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="通过router-link和router-view就可以展示页面"><a href="#通过router-link和router-view就可以展示页面" class="headerlink" title="通过router-link和router-view就可以展示页面"></a>通过router-link和router-view就可以展示页面</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 跳转页面 --&gt;</span><br><span class="line">// 和a标签很类似</span><br><span class="line">&lt;router-link to=&quot;/right&quot;&gt;&lt;/router-link&gt;</span><br><span class="line">&lt;!-- 映射路由 --&gt;</span><br><span class="line">// &lt;router-view&gt;组件，用于映射跳转页面（相当于一个容器）</span><br><span class="line">可以放在任何的地方</span><br><span class="line">&lt;router-view&gt;123&lt;/router-view&gt;</span><br></pre></td></tr></table></figure>
<h2 id="过程"><a href="#过程" class="headerlink" title="过程"></a>过程</h2><p>先配置路由（可以是静态路由，也可以是动态），实例化路由（router）并传入routes（路由表），mode（模式）等，然后导出router，在main.js中注册router。然后就可以使用router-view（类似slot）是一个容器，router-link用于跳转，类似a标签</p>
<h2 id="查看路由信息"><a href="#查看路由信息" class="headerlink" title="查看路由信息"></a>查看路由信息</h2><p>在一个组件内部调用router的api，通过this.$route查看有哪些api(这已经被挂载在vue原型上了)，对应当前组件的路由。<br>this.$touter就是vue-router的一个实例（相当于new Vue，实例化后，也会生成一个全局的router），对应router的全局配置</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/" data-id="cl127vs1s00004sw65n0oc7mf" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/03/22/vuew-router%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          vuew-router的基本使用
        
      </div>
    </a>
  
  
    <a href="/2022/03/20/axios%E5%AD%A6%E4%B9%A0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">axios学习</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/">vuex学习</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vuew-router%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/">vuew-router的基本使用</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/">vue-router的创建过程</a>
          </li>
        
          <li>
            <a href="/2022/03/20/axios%E5%AD%A6%E4%B9%A0/">axios学习</a>
          </li>
        
          <li>
            <a href="/2022/03/16/%E4%BA%86%E8%A7%A3vue%E7%BB%84%E4%BB%B6/">了解vue组件</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2022 诶无痕和诶有痕<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>